package com.stx.boke.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.stx.boke.dao.UserDao;
import com.stx.boke.dao.impl.UserDaoImpl;
import com.stx.boke.entity.User;


public class CheckLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private UserDao ud = new UserDaoImpl(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		
		String uname = request.getParameter("uname");
		String password = request.getParameter("password");
//		if (uname==null||password==null){
//			request.setAttribute("messa", "用户登录名或密码不能为空!");
//		}

		User user = new User();
		user.setUname(uname);
		user.setPassword(password);

		User loginuser = ud.getUser(user);
	
		if(loginuser!=null){   
			HttpSession session = request.getSession();
			session.setAttribute("USER", loginuser); 
			request.getRequestDispatcher("index.jsp").forward(request, response);
			//getServletContext().getRequestDispatcher("index.jsp").forward(request, response);
			//response.sendRedirect("index.jsp");		
		}else{
			request.setAttribute("messages", "用户登录名或密码错误!");
			getServletContext().getRequestDispatcher("/jsp/login.jsp").forward(request, response);
			//response.sendRedirect("/kqblog/jsp/login.jsp");
			
			 
			 
			
		}	
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}
